package code.lewdy.mail;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

/**
 * 邮件接收工具类
 * 
 * @author Administrator
 * 
 */
public class MailReceiver {

	private String host = "imap.sina.com"; // imap.qq.com
	private String user = "lewdy007@sina.com"; // 506641796@qq.com
	private String password = "lewdy123"; // c==456java
	private String mbox = "INBOX";
	private String protocol = "imap"; // imap
	private boolean debug = false;

	private Store store = null;
	private Folder folder = null; // 邮件文件夹
	Message[] msgs = null; //邮件列表
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MailReceiver mr = new MailReceiver("imap.sina.com", "lewdy007@sina.com", "lewdy123",
				"INBOX", "imap");
		mr.fetchSubjectMessagesDESC("工商", 5);
	}

	/**
	 * 构造函数
	 * 
	 * @param host
	 * @param user
	 * @param password
	 * @param mbox
	 * @param protocol
	 * @param subject
	 */
	public MailReceiver(String host, String user, String password, String mbox,
			String protocol) {
		this.host = host;
		this.user = user;
		this.password = password;
		this.mbox = mbox;
		this.protocol = protocol;
	}

	public void openFolder() {
		try {
			// Get a Properties object 系统属性
			Properties props = System.getProperties();

			// Get a Session object
			// 邮件会话对象
			Session session = Session.getInstance(props, null);
			session.setDebug(debug);

			if (protocol != null)
				store = session.getStore(protocol);
			else
				store = session.getStore();

			// Connect
			if (host != null || user != null || password != null) {

				store.connect(host, user, password);
			}

			// Open the Folder

			folder = store.getDefaultFolder();

			if (folder == null) {
				System.out.println("Cant find default namespace");
				System.exit(1);
			}

			folder = folder.getFolder(mbox);

			if (folder == null) {
				System.out.println("Invalid folder");
				System.exit(1);
			}

			folder.open(Folder.READ_ONLY);
		} catch (Exception e) {
			System.out.println("Mail=>" + e.getMessage());
			e.printStackTrace();
		}finally{
			//System.out.println("Finally...");
		}
	}

	/**
	 * 提取与subject相关的邮件-倒序
	 * 
	 * @param fetchSize
	 * @return
	 */
	public List<Message> fetchSubjectMessagesDESC(String subject, int fetchSize) {
		List<Message> msgList = new ArrayList<Message>();
		if(subject == null)
			subject = "";
		openFolder();
		if (folder == null)
			return msgList;
		try {
			msgs = folder.getMessages();
			int count = 1;
			int i =0;
			for(i=msgs.length-1; i>=0; i--, count++){
				if(count > fetchSize)
					break;
				if(msgs[i].getSubject().contains(subject)){
					msgList.add(msgs[i]);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("ICBC Mail size:"+msgList.size());
		return msgList;
	}
	
	/**
	 * 关闭连接
	 */
	public void close(){
		if(folder!=null && folder.isOpen())
			try {
				folder.close(false);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if(store!=null && store.isConnected())
			try {
				store.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

}
